package com.uchiha.nova.mainService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.entity.ChinaAreaEntity;
import com.uchiha.nova.core.model.query.ChinaAreaQuery;
import com.uchiha.nova.core.model.vo.ChinaAreaVO;
import com.uchiha.nova.core.model.vo.ChinaGeoJsonVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.cache.ChinaAreaCache;
import com.uchiha.nova.mainService.service.IChinaAreaService;
import com.uchiha.nova.mainService.service.IChinaGeoJsonService;
import com.uchiha.nova.mainService.wrapper.ChinaAreaWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 行政设置 控制器
 *
 * @author magic
 */
@RestController
@AllArgsConstructor
@RequestMapping("/chinaArea")
@Tag(name = "行政设置")
@Slf4j
public class ChinaAreaController extends BaseController {

	@Resource
	IChinaAreaService chinaAreaService;
	@Resource
	IChinaGeoJsonService chinaGeoJsonService;


	/**
	 * 详情
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_DETAIL)
	@Operation(summary = "详情")
	public R<ChinaAreaVO> detail(@RequestBody ChinaAreaEntity chinaAreaEntity) {
		ChinaAreaEntity detail = chinaAreaService.getOne(Condition.getQueryWrapper(chinaAreaEntity));
		return R.data(ChinaAreaWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping(NovaCRUDMethodConstant.NOVA_LIST)
	@Operation(summary = "分页查询")
	public R getPage(ChinaAreaQuery query, QueryContext queryContext) {
		IPage<ChinaAreaEntity> pages = chinaAreaService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext,ChinaAreaEntity.class));
		return R.data(ChinaAreaWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVE)
	@Operation(summary = "新增")
	public R save(@RequestBody ChinaAreaEntity chinaAreaEntity) {
		return R.status(chinaAreaService.save(chinaAreaEntity));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_UPDATE)
	@Operation(summary = "修改")
	public R update(@RequestBody ChinaAreaEntity chinaAreaEntity) {
		return R.status(chinaAreaService.updateById(chinaAreaEntity));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVEORUPDATE)
	@Operation(summary = "新增或修改")
	public R saveOrUpdate(@RequestBody ChinaAreaEntity chinaAreaEntity) {
		return R.status(chinaAreaService.saveOrUpdate(chinaAreaEntity));
	}


	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_REMOVE)
	@Operation(summary = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.status(chinaAreaService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 物理删除，不可恢复
	 */
	@Operation(summary = "物理删除")
	@DeleteMapping(NovaCRUDMethodConstant.NOVA_DELETE)
	public R delete(@RequestParam String ids){
		return R.status(chinaAreaService.deletePhysics(Func.toLongList(ids)));
	}

	@Operation(summary = "懒加载")
	@GetMapping("/tree/lazy")
	public R treeLazy(@RequestParam(defaultValue = "0") Integer id){
		return chinaAreaService.treeLazyChinaArea(id);
	}

	@Operation(summary = "懒加载")
	@GetMapping("/tree")
	public R tree(){
		return R.data(chinaAreaService.tree());
	}


	@GetMapping("/geo")
	public R getGeo(@RequestParam Long id){

		ChinaGeoJsonVO chinaGeoJson = ChinaAreaCache.getChinaGeoJson(id);
		List<Long> list = new ArrayList<>();

		list = getFull(list,id);
		Map<String,Object> map = new HashMap<>();
		map.put("geoJson",chinaGeoJson);
		map.put("selectMap",list);
		return R.data(map);
	}

	public List<Long> getFull(List<Long> list,Long id){
		list.add(id);
		ChinaAreaEntity chinaAreaEntity = chinaAreaService.getById(id);
		if(chinaAreaEntity.getParentId().compareTo(0L)!=0){
			this.getFull(list,chinaAreaEntity.getParentId());
		}
		List<Long> list1 = list.stream().sorted().collect(Collectors.toList());
		return list1;
	}

}
